package cn.gdcp.lkqapp06;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private TextView txtResult;
    private EditText editKey;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtResult = findViewById(R.id.txt_result);
        editKey = findViewById(R.id.edt_key);

        btn1 = findViewById(R.id.btn_volleyget);
        btn2 = findViewById(R.id.btn_volleypost);
        btn3 = findViewById(R.id.btn_volleyJsonRequest);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                useVolleyStringRequest();
            }
        });


        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = editKey.getText().toString();
                useVolleyStringRequestPost(phone);
            }
        });


        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String cityCode = editKey.getText().toString();
                useVolleyJsonRequest(cityCode);
            }
        });
    }

    /*101280106*/
    /*101282103*/
    private void useVolleyJsonRequest(String cityCode) {
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        String url = "http://t.weather.sojson.com/api/weather/city/" + cityCode;
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e(TAG,jsonObject.toString());
                try {
                    String time = jsonObject.getString("time");
                    String city = jsonObject.getJSONObject("cityInfo").getString("city");
                    String cd = jsonObject.getJSONObject("data").getJSONArray("forecast").getString(0);
                    String[] aa = cd.split("\"");
                    String type = null;
                    for (int i = 0; i<aa.length; i++){
                        if (aa[i].equals("type")){
                            Log.e(TAG,aa[i+2]);
                            type = aa[i+2];
                        }
                    }
                    int pm25 = jsonObject.getJSONObject("data").getInt("pm25");
                    String wendu = jsonObject.getJSONObject("data").getString("wendu");

                    txtResult.setText("时间：" + time + "\n" + "城市：" + city + "\n"
                            + "天气：" + type + "\n" + "PM2.5：" + pm25 + "\n" + "温度：" + wendu);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG,volleyError.getMessage(),volleyError);
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

    private void useVolleyStringRequestPost(final String phone) {
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        String url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
        StringRequest stringRequest = new StringRequest(Request.Method.POST,url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.e(TAG,s);
                txtResult.setText(s);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG,volleyError.toString());
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("mobileCode",phone);
                map.put("userID","");
                return map;
            }
        };
        requestQueue.add(stringRequest);
    }

    private void useVolleyStringRequest() {
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        String url =  "https://blog.csdn.net/guolin_blog/article/details/17482095";
        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.e(TAG,s);
                txtResult.setText(s);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG,volleyError.toString());
            }
        });
        requestQueue.add(stringRequest);
    }
}
